package scau.erps.view.system.team;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import scau.erps.app.Erps;
import scau.erps.entity.Bidding;
import scau.erps.service.BiddingService;
import scau.erps.service.ResearchService;
import scau.erps.service.ScoreService;
import scau.erps.service.impl.BiddingServiceImpl;
import scau.erps.service.impl.ResearchServiceImpl;
import scau.erps.service.impl.ScoreServiceImpl;
import scau.erps.view.system.teacher.SystemOrderPanelTemplate;

/**
 * 系统订单竞价——小组
 * @author DCJ
 *
 */
public class MySystemOrderBiddingPanel extends SystemOrderPanelTemplate {	
	private static final long serialVersionUID = 1L;
	 private static final int[] PRICE={2,4,4,5};//每个产品的估计最低成本,用来计算最低报价
	public MySystemOrderBiddingPanel(){
		super();
		this.deleteBtn.setVisible(false);
		this.yearCombo.setVisible(false);
		this.endBiddingBtn.setVisible(false);
		//获取小组竞价资格(产品、市场、ISO) 
		int team_Id=Erps.getCurrentTeam().getId();
		//ISO
		Boolean ISO9000=false,ISO14000=false;
	    ResearchService researchService = Erps.getIocInstance().get(
						ResearchServiceImpl.class);
		if (researchService.findResearchs(team_Id, "ISO9000") == 1) ISO9000=true;
		if (researchService.findResearchs(team_Id, "ISO14000") == 2) ISO14000=true;
		//市场
		Boolean district=false,domestic=false,asia=false,internation=false;
		if (researchService.findResearchs(team_Id, "区域") == 1) district=true;
		if (researchService.findResearchs(team_Id, "国内") == 2) domestic=true;
		if (researchService.findResearchs(team_Id, "亚洲") == 3) asia=true;
		if (researchService.findResearchs(team_Id, "国际") == 4) internation=true;
		//产品
		Boolean crystal=false,ruby=false,sapphire=false;
		if (researchService.findResearchs(team_Id, "Crystal") == 4) crystal=true;
		if (researchService.findResearchs(team_Id, "Ruby") == 6) ruby=true;
		if (researchService.findResearchs(team_Id, "Sapphire") == 8) sapphire=true;
				
	    this.dataModel.showAvailableOrder(ISO9000, ISO14000, district, domestic, asia, internation, crystal, ruby, sapphire);						
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==filterBtn){
			final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dataModel);
			List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
			String year = null;
			if(yearCombo.getSelectedIndex() != 0 ){
				year = (String) yearCombo.getSelectedItem();
			}
			String product = null;
			if(productCombo.getSelectedIndex() != 0 ){
				product = (String) productCombo.getSelectedItem();
			}
			String market = null;
			if(marketCombo.getSelectedIndex() != 0 ){
				market = (String) marketCombo.getSelectedItem();
			}
			if(year !=null){
				filters.add(RowFilter.regexFilter(year));
			}
			if(product !=null){
				filters.add(RowFilter.regexFilter(product));
			}
			if(market !=null){
				filters.add(RowFilter.regexFilter(market));
			}
			sorter.setRowFilter(RowFilter.andFilter(filters));
			this.table.setRowSorter(sorter); 
            this.table.setModel(dataModel);			
		}
		else if(e.getSource() ==endBiddingBtn){
		}
		else if(e.getSource() == biddingBtn){
			int row = this.table.getSelectedRow();
			if (row == -1) {
				JOptionPane.showMessageDialog(this, "未选中任何行");
			} else {
				int lowestPrice=0;
				int amount=(int)this.table.getValueAt(row, 4);
				switch((String)this.table.getValueAt(row, 2)){
				case "Beryl" :lowestPrice=PRICE[0]*amount+1;break;
				case "Crystal" :lowestPrice=PRICE[1]*amount+1;break;
				case "Ruby" :lowestPrice=PRICE[2]*amount+1;break;
				case "Sapphire" :lowestPrice=PRICE[3]*amount+1;break;
				default :JOptionPane.showMessageDialog(null, "最低竞价出错","提示",JOptionPane.INFORMATION_MESSAGE);
				}
				int order_Id = (int) this.table.getValueAt(row, 0);
				double price = (double) this.table.getValueAt(row, 7);
				String year = (String) this.table.getValueAt(row, 1);
				BiddingService service = Erps.getIocInstance().get(BiddingServiceImpl.class);
				ScoreService scoreService = Erps.getIocInstance().get(ScoreServiceImpl.class);
				int team_Id = Erps.getCurrentTeam().getId();
				List<Bidding> list = service.getBiddings(team_Id, order_Id);
				if(list.size()>0){
					JOptionPane.showMessageDialog(this, "该订单已竞价，如需修改请先在我的竞价窗口删除，再对该订单竞价");
				}else{
					String str = JOptionPane.showInputDialog("每笔竞价需支付广告费1M,请输入整数竞价,不得高于参考价"+price);
					if(str != null || "".equals(str)){
						if(str.matches("[0-9]+")){
							int myprice = Integer.parseInt(str);
							if(myprice > price||myprice<lowestPrice){
								JOptionPane.showMessageDialog(this, "竞价为正整数，且低于或等于参考价并大于等于该订单最低报价"+lowestPrice+"M");
							}
							else{														
								Bidding bidding = new Bidding();
								bidding.setTeam_Id(team_Id);
								bidding.setYear(year);
								bidding.setOrder_Id(order_Id);
								bidding.setMyprice(myprice);
								double score = scoreService.getScore(team_Id).get(0).getScore();
								bidding.setScore(score);
								bidding.setStatus("已报价");							
								service.save(bidding);
								JOptionPane.showMessageDialog(this, "竞价成功并支付广告费1M,可在我的竞价窗口点击刷新查看");
							}					
						}else{
							JOptionPane.showMessageDialog(this, "请输入正整数");
						}
					}
				}									
			}
		}		
	}
}
